package com.howells.time.module.register.presenter;


import com.howells.time.base.http.HttpURL;
import com.howells.time.base.http.HttpUtils;
import com.howells.time.base.http.JsonCallback;
import com.howells.time.module.register.view.ResigisterView;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2016.4.6.
 */
public class ResigisterPresenterImp implements ResigisterPresenter {
    private ResigisterView resigisterView;

    public ResigisterPresenterImp(ResigisterView resigisterView) {
        this.resigisterView = resigisterView;
    }

    @Override
    public void resigister(String phone, String code, String psw) {
        Map<String,String> info=new HashMap<>();
        info.put("username",phone);
        info.put("validateCode",code);
        info.put("password",psw);
        Logger.d(HttpURL.ResigisterApi.toString());
        HttpUtils.PostObject(HttpURL.ResigisterApi.toString(), info, new JsonCallback() {
            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                resigisterView.resultMsg("网络异常!");
            }

            @Override
            public void onResponse(Map<String, Object> response,int id) {
                if(response.get("message").equals("success"))
                    resigisterView.Success();
                else {
                    Logger.d(response);
                    resigisterView.resultMsg(response.get("message").toString());
                }
            }
        });
    }

    @Override
    public void getVerifyCode(String phone) {
        HttpUtils.Get(HttpURL.VerifyCodeApi.toString() + phone, new JsonCallback() {
            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                resigisterView.resultMsg("网络异常!");
            }

            @Override
            public void onResponse(Map<String, Object> response,int id) {
                if(response.get("message").equals("success")) {
                    resigisterView.resultMsg("验证码获取成功");
                }
                else {
                    Logger.d(response);
                    resigisterView.resultMsg("获取验证码异常");
                }
            }
        });
    }

    @Override
    public void forgetpsw(String phone, String code, String psw) {
        Map<String,String> info=new HashMap<>();
        info.put("phoneNum",phone);
        info.put("verifyCode",code);
        info.put("newPwd",psw);
        HttpUtils.PostObject(HttpURL.ForgetPswApi.toString(), info, new JsonCallback() {
            @Override
            public void onError(Call call, Exception e,int id) {
                resigisterView.resultMsg("网络异常!");
            }

            @Override
            public void onResponse(Map<String, Object> response,int id) {
                if(response==null)
                    resigisterView.Success();
                else {
                    if(response.get("errorCode").equals("29"))
                        resigisterView.resultMsg("验证码错误");
                    else
                        resigisterView.resultMsg(response.toString());
                }
            }
        });
    }
}
